package yawning.mew.character;

import java.util.Properties;

import yawning.mew.PropertiesBase;
import yawning.mew.PropertiesBase.IProperty;

public class PropertiesToonBuilder {
    public enum Property implements IProperty {
        LEVEL("Level", "0"),
        CLASS("Class", "Druid"),
        TALENTSPEC("TalentSpec", "Druid.Feral"),

        STRENGTH("Strength", "0"),
        AGILITY("Agility", "0"),
        STAMINA("Stamina", "0"),
        INTELLECT("Intellect", "0"),
        SPIRIT("Spirit", "0"),
        ARMOR("Armor", "0"),

        ATTACKPOWER("AttackPower", "0"),
        HASTERATING("HasteRating", "0"),
        CRITRATING("CritRating", "0"),
        HITRATING("HitRating", "0"),
        EXPERTISERATING("ExpertiseRating", "0"),
        MASTERYRATING("MasteryRating", "0"),

        WEAPONTOP("WeaponTop", "0"),
        WEAPONBOTTOM("WeaponBottom", "0"),
        WEAPONSPEED("WeaponSpeed", "0"),

        TIER_10_2PC("Tier_10_2pc", "false"),
        TIER_10_4PC("Tier_10_4pc", "false"),

        TIER_11_2PC("Tier_11_2pc", "false"),
        TIER_11_4PC("Tier_11_4pc", "false"),
        
        ARMORSPEC("ArmorSpec", "true"),
        METAGEM("MetaGem", "none");

        private final String propertyKey;
        private final String defaultValue;

        private Property(String k, String v) {
            propertyKey = k;
            defaultValue = v;
        }

        public String getKey() {
            return this.propertyKey;
        }

        public String getDefault() {
            return this.defaultValue;
        }
    }
    
    private PropertiesBase mProperties;
    
    public PropertiesToonBuilder setProperties(Properties props) {
    	mProperties = new PropertiesBase(props);
    	return this;
    }
    
    public PropertiesToonBuilder setConfig(PropertiesBase config) {
    	mProperties = config;
    	return this;
    }
    
    public Toon build() {
    	Toon c = new Toon();
    	c.level = mProperties.getInt(Property.LEVEL);
    	c.strength = mProperties.getInt(Property.STRENGTH);
    	c.agility = mProperties.getInt(Property.AGILITY);
    	c.stamina = mProperties.getInt(Property.STAMINA);
    	c.intellect = mProperties.getInt(Property.INTELLECT);
    	c.spirit = mProperties.getInt(Property.SPIRIT);
    	c.armor = mProperties.getInt(Property.ARMOR);
    	c.attackPower = mProperties.getInt(Property.ATTACKPOWER);
    	c.hasteRating = mProperties.getInt(Property.HASTERATING);
    	c.critRating = mProperties.getInt(Property.CRITRATING);
    	c.hitRating = mProperties.getInt(Property.HITRATING);
    	c.expertiseRating = mProperties.getInt(Property.EXPERTISERATING);
    	c.masteryRating = mProperties.getInt(Property.MASTERYRATING);
    	c.weaponTop = mProperties.getInt(Property.WEAPONTOP);
    	c.weaponBottom = mProperties.getInt(Property.WEAPONBOTTOM);
    	c.weaponSpeed = mProperties.getInt(Property.WEAPONSPEED);
    	c.tier_10_2pc = mProperties.getBool(Property.TIER_10_2PC);
    	c.tier_10_4pc = mProperties.getBool(Property.TIER_10_4PC);
    	c.tier_11_2pc = mProperties.getBool(Property.TIER_11_2PC);
    	c.tier_11_4pc = mProperties.getBool(Property.TIER_11_4PC);
    	c.isArmorSpec = mProperties.getBool(Property.ARMORSPEC);
    	
    	String metagem = mProperties.getString(Property.METAGEM);
    	for (MetaGem gem : MetaGem.values()) {
    		if (metagem.equals(gem.getKey())) {
    			c.mMetaGem = gem;
    			break;
    		}
    	}
    	
    	c.mClass = Clazz.get(mProperties.getString(Property.CLASS));
    	for (Glyph g : c.mClass.getGlyphs()) {
    		String glyphProp = g.mName.replace(' ', '_');
    		if (mProperties.getProperties().get(glyphProp).toString() == "true") c.glyphs.add(g); 
    	}
    	for (Talent t : c.mClass.getTalents()) {
    		String talentProp = t.mName.replace(' ', '_');
    		int n = Integer.parseInt(mProperties.getProperties().getProperty(talentProp, "0"));
			if (n > 0) c.talents[t.mTalentNumber] = (byte)n;
		}
		for (TalentTree t : c.mClass.getTalentTrees())  {
			if (mProperties.getString(Property.TALENTSPEC).equals(t.mName)) {
				c.mTalentSpec = t;
				break;
			}
		}	
    	
    	return c;
    }
}
